﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace NormReference.Services
{
    public class CSVReader
    {
        public static CSVReader instance;

        private CSVReader()
        {
 
        }

        public static CSVReader getInstance()
        {
            if (instance == null)
            {
                instance = new CSVReader();
            }
            return instance;
        }

        public List<string[]> parseCSV(string path)
        {
            List<string[]> parsedData = new List<string[]>();
            StreamReader readFile = new StreamReader(path);

            try
            {
                string line;
                string[] row;

                while ((line = readFile.ReadLine()) != null)
                {
                    row = line.Split(',');
                    parsedData.Add(row);
                }
            }
            catch (Exception e)
            {
                Logger.getInstance().printToLogFile("Exception in CSVReader: " + e.Message);
            }
            finally 
            {
                readFile.Close();
            }
            return parsedData;
        }

        public void ClearCSV(string path)
        {
            FileStream file = new FileStream(path, FileMode.Create);
            StreamWriter writer = new StreamWriter(file);
            writer.Write("");
            writer.Close();
        }
    }
}